package com.xiaoniu.forum.service.impl;

import com.xiaoniu.forum.common.constant.ForumConstant;
import com.xiaoniu.forum.common.utils.ESQueryUtils;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.dao.es.SearchDao;
import com.xiaoniu.forum.entity.CommunityTopicEntity;
import com.xiaoniu.forum.service.TopicSearchService;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class TopicSearchServiceTest implements TopicSearchService {
    private static final String INDEX_NAME = "topic";
    private static final String[] MULTI_MATCH_FIELDS = {"title","threadsText"};
    @Autowired
    private SearchDao searchDao;

    /**
     *  根据关键字分页查询话题
     *  最后分页的数据都存储在了分页对象中
     */
    @Override
    public void search(String keyword, PageUtils page) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //多字段检索
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery(keyword,MULTI_MATCH_FIELDS));
        //检索的字段进行高亮显示
        List<String> fields = Arrays.asList(MULTI_MATCH_FIELDS);
        HighlightBuilder highlightBuilder = ESQueryUtils.getHighlightBuilder(fields);
        searchDao.highlightSearch(INDEX_NAME,searchSourceBuilder,highlightBuilder,page,CommunityTopicEntity.class);
    }

    //TODO 保存到es的字段，如果是富文本字段并且是需要进行检索的字段，需要把富文本的标签去除后存储到es
    @Override
    public boolean save(CommunityTopicEntity topic) {
        if (!topic.getPass().equals("1")){//如果是未通过审核的话题，不进行保存
            return false;
        }
        removeRichText(topic);
        return searchDao.save(INDEX_NAME,topic.getId(),topic);
    }

    @Override
    public boolean updateById(CommunityTopicEntity topic) {
        return searchDao.updateById(INDEX_NAME,topic.getId(),topic);
    }

    @Override
    public void bulkUpdateById(List<CommunityTopicEntity> topics) {
        topics.forEach((topic)->removeRichText(topic));
        searchDao.bulkUpdateById(INDEX_NAME,topics);
    }

    /**
     *  批量将话题保存到es中，只有审核通过的话题才可以检索
     */
    //TODO 保存到es的字段，如果是富文本字段并且是需要进行检索的字段，需要把富文本的标签去除后存储到es
    //TODO 不然的话，会出现搜索 "img" 就会把包含img的富文本都检索出来了，这是不对的
    //TODO 同时，搜索的时候，我们并不需要展示富文本，所以去除富文本标签是没有什么问题的
    @Override
    public void bulkSave(List<CommunityTopicEntity> topics) {
        List<CommunityTopicEntity> passTopics = new ArrayList<>();
        for (CommunityTopicEntity topic:topics){
            if (topic.getPass().equals("1")){
                removeRichText(topic);
                passTopics.add(topic);
            }
        }
        searchDao.bulkSave(INDEX_NAME,passTopics);
    }

    /**
     * 去除话题里的富文本，替换成纯文本
     */
    private void removeRichText(CommunityTopicEntity topic){
        String text = Jsoup.parse(topic.getThreadsText()).text();
        topic.setThreadsText(text);
    }
}
